package com.cetc39.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.cetc39.bean.Project;
import com.cetc39.dao.ProjectDao;

public class ProjectDaoImpl extends HibernateDaoSupport implements ProjectDao {

	@Override
	public void save(Project p) {
		this.getHibernateTemplate().save(p);
	}

	@Override
	public void delete(int id) {
		Project p = this.getHibernateTemplate().load(Project.class, id);
		this.getHibernateTemplate().delete(p);
	}

	@Override
	public void update(Project p) {
		this.getHibernateTemplate().update(p);
	}

	@Override
	public List<?> findAll() {
		List<?> pList = this.getHibernateTemplate().find("FROM Project");
		return pList;
	}


	
	@Override
	public Project findById(int id) {
		Project p = this.getHibernateTemplate().get(Project.class, id);
		return p;
	}

	@Override
	public List<?> findByProjectId() {
		return null;
	}

	@Override
	public List<?> findByConditions(String[] conditions) {
		StringBuffer queryString = new StringBuffer();
		queryString.append("FROM Project WHERE 1=1");
		if(conditions[0] != null && !"".equals(conditions[0])) {
			queryString.append(" AND leader_39.id=" + conditions[0]);
		}
		if(conditions[1] != null && !"".equals(conditions[1])) {
			queryString.append(" AND head_dept.id=" + conditions[1]);
		}
		if(conditions[2] != null && !"".equals(conditions[2])) {
			queryString.append(" AND project_filed.id=" + conditions[2]);
		}
		if(conditions[3] != null && !"".equals(conditions[3])) {
			queryString.append(" AND project_status=" + conditions[3]);
		}
		if(conditions[4] != null && !"".equals(conditions[4])) {
			queryString.append(" AND time_details=" + conditions[4]);
		}
		if(conditions[5] != null && !"".equals(conditions[5])) {
			queryString.append(" AND client.id=" + conditions[5]);
		}
		if(conditions[6] != null && !"".equals(conditions[6])) {
			queryString.append(" AND project_code='" + conditions[6] + "'");
		}
		System.out.println(queryString);
		return this.getHibernateTemplate().find(queryString.toString());
	}

	@Override
	public List<?> findByCpdps(int user_id) {
		List<?> pList = this.getHibernateTemplate().find("FROM Project WHERE cpd_ps.id=" + user_id);
		return pList;
	}

	@Override
	public List<?> findByManager(int user_id) {
		List<?> pList = this.getHibernateTemplate().find("FROM Project WHERE project_manager.id=" + user_id);
		return pList;
	}


	
}
